package com.cec.utils;

import com.cec.entity.Info;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class JsoupUtil {

    public static List<String> parseUrlList(String baseURL, String doc) {
        Document document = Jsoup.parse(doc);
        Elements topList = document.getElementsByClass("top_list");
        Elements prodYingcaiList = document.getElementsByClass("prod_yingcai_list");
        ArrayList<String> urlList = new ArrayList<>();
        for (Element element : topList) {
            Elements a = element.getElementsByTag("a");
            a.forEach(a1 -> urlList.add(baseURL+ a1.attr("href")));
        }

        for (Element element : prodYingcaiList) {
            Elements a = element.getElementsByTag("a");
            a.forEach(a1 -> urlList.add(baseURL+ a1.attr("href")));
        }
        // List集合去重
        List<String> newUrlList = urlList.stream().distinct().collect(Collectors.toList());
        return newUrlList;
    }

    public  static Info getInfo(String doc){
        Info info = new Info();
        Document document = Jsoup.parse(doc);

        Elements detailTitle = document.getElementsByClass("detail_title");
        //System.out.println("detailTitle = " + detailTitle.get(0));
        Elements detailInfo = document.getElementsByClass("detail_info");
        //System.out.println("detailInfo = " + detailInfo.get(0));

        Elements detailContent = document.getElementsByClass("detail_content");
        //System.out.println("detailContent = " + detailContent.get(0));

        info.setInfoTitle(detailTitle.text());
        info.setInfo(detailInfo.text());
        info.setInfoContent(String.valueOf(detailContent.get(0)));

        return  info;
    }
}
